package cn.harry.cabinet.theme.components

import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat
import cn.harry.cabinet.R
import cn.harry.cabinet.theme.constants.ThemeConstants
import cn.harry.cabinet.utils.ColorUtils

/**
 * 语义化按钮组件
 * 支持Success、Warning、Danger、Info、Neutral、Primary等语义化按钮样式
 * 
 * 特性：
 * - 语义化颜色支持
 * - 主题自适应
 * - 多种按钮变体 (Primary, Secondary)
 * - 自动文本颜色适配
 * - 无障碍支持
 */
class SemanticButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatButton(context, attrs, defStyleAttr) {
    
    /**
     * 按钮类型枚举
     */
    enum class ButtonType {
        PRIMARY,
        SUCCESS,
        WARNING,
        DANGER,
        INFO,
        NEUTRAL
    }
    
    /**
     * 按钮变体枚举
     */
    enum class ButtonVariant {
        PRIMARY,    // 实心按钮
        SECONDARY   // 边框按钮
    }
    
    private var buttonType: ButtonType = ButtonType.PRIMARY
    private var buttonVariant: ButtonVariant = ButtonVariant.PRIMARY
    
    init {
        // 设置默认样式
        applySemanticStyle()
    }
    
    /**
     * 设置按钮类型
     */
    fun setButtonType(type: ButtonType, variant: ButtonVariant = ButtonVariant.PRIMARY) {
        this.buttonType = type
        this.buttonVariant = variant
        applySemanticStyle()
    }
    
    /**
     * 应用语义化样式
     */
    private fun applySemanticStyle() {
        val textColor = getTextColor()
        
        // 设置背景
        background = ContextCompat.getDrawable(context, getBackgroundDrawable())
        
        // 设置文本颜色
        setTextColor(textColor)
        
        // 设置内边距
        val paddingHorizontal = resources.getDimensionPixelSize(R.dimen.button_padding_horizontal)
        val paddingVertical = resources.getDimensionPixelSize(R.dimen.button_padding_vertical)
        setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical)
        
        // 设置最小高度
        minHeight = resources.getDimensionPixelSize(R.dimen.button_min_height)
        
        // 启用状态变化动画
        isClickable = true
        isFocusable = true
    }
    
    /**
     * 获取背景drawable资源
     */
    private fun getBackgroundDrawable(): Int {
        return when (buttonType) {
            ButtonType.SUCCESS -> when (buttonVariant) {
                ButtonVariant.PRIMARY -> R.drawable.button_success_primary
                ButtonVariant.SECONDARY -> R.drawable.button_success_secondary
            }
            ButtonType.WARNING -> when (buttonVariant) {
                ButtonVariant.PRIMARY -> R.drawable.button_warning_primary
                ButtonVariant.SECONDARY -> R.drawable.button_warning_secondary
            }
            ButtonType.DANGER -> when (buttonVariant) {
                ButtonVariant.PRIMARY -> R.drawable.button_danger_primary
                ButtonVariant.SECONDARY -> R.drawable.button_danger_secondary
            }
            ButtonType.INFO -> when (buttonVariant) {
                ButtonVariant.PRIMARY -> R.drawable.button_info_primary
                ButtonVariant.SECONDARY -> R.drawable.button_info_secondary
            }
            ButtonType.NEUTRAL -> when (buttonVariant) {
                ButtonVariant.PRIMARY -> R.drawable.button_neutral_primary
                ButtonVariant.SECONDARY -> R.drawable.button_neutral_secondary
            }
            ButtonType.PRIMARY -> when (buttonVariant) {
                ButtonVariant.PRIMARY -> R.drawable.button_tech_blue_primary
                ButtonVariant.SECONDARY -> R.drawable.button_tech_blue_secondary
            }
        }
    }
    
    /**
     * 获取文本颜色
     */
    private fun getTextColor(): ColorStateList {
        val enabledColor = when (buttonVariant) {
            ButtonVariant.PRIMARY -> {
                // Primary按钮使用白色文本
                ContextCompat.getColor(context, android.R.color.white)
            }
            ButtonVariant.SECONDARY -> {
                // Secondary按钮使用对应的语义化颜色
                when (buttonType) {
                    ButtonType.SUCCESS -> ContextCompat.getColor(context, R.color.semantic_success)
                    ButtonType.WARNING -> ContextCompat.getColor(context, R.color.semantic_warning)
                    ButtonType.DANGER -> ContextCompat.getColor(context, R.color.semantic_danger)
                    ButtonType.INFO -> ContextCompat.getColor(context, R.color.semantic_info)
                    ButtonType.NEUTRAL -> ContextCompat.getColor(context, R.color.neutral_700)
                    ButtonType.PRIMARY -> ContextCompat.getColor(context, R.color.theme_primary)
                }
            }
        }
        
        val disabledColor = ContextCompat.getColor(context, R.color.neutral_text_disabled)
        
        return ColorStateList(
            arrayOf(
                intArrayOf(-android.R.attr.state_enabled), // 禁用状态
                intArrayOf() // 默认状态
            ),
            intArrayOf(disabledColor, enabledColor)
        )
    }
    
    /**
     * 设置成功按钮样式
     */
    fun setAsSuccessButton(variant: ButtonVariant = ButtonVariant.PRIMARY) {
        setButtonType(ButtonType.SUCCESS, variant)
    }
    
    /**
     * 设置警告按钮样式
     */
    fun setAsWarningButton(variant: ButtonVariant = ButtonVariant.PRIMARY) {
        setButtonType(ButtonType.WARNING, variant)
    }
    
    /**
     * 设置危险按钮样式
     */
    fun setAsDangerButton(variant: ButtonVariant = ButtonVariant.PRIMARY) {
        setButtonType(ButtonType.DANGER, variant)
    }
    
    /**
     * 设置信息按钮样式
     */
    fun setAsInfoButton(variant: ButtonVariant = ButtonVariant.PRIMARY) {
        setButtonType(ButtonType.INFO, variant)
    }
    
    /**
     * 设置中性按钮样式
     */
    fun setAsNeutralButton(variant: ButtonVariant = ButtonVariant.PRIMARY) {
        setButtonType(ButtonType.NEUTRAL, variant)
    }
    
    /**
     * 设置主色按钮样式
     */
    fun setAsPrimaryButton(variant: ButtonVariant = ButtonVariant.PRIMARY) {
        setButtonType(ButtonType.PRIMARY, variant)
    }
    
    /**
     * 获取当前按钮类型
     */
    fun getButtonType(): ButtonType = buttonType
    
    /**
     * 获取当前按钮变体
     */
    fun getButtonVariant(): ButtonVariant = buttonVariant
    
    /**
     * 检查按钮颜色对比度是否符合无障碍标准
     */
    fun isAccessibilityCompliant(): Boolean {
        val backgroundColor = when (buttonType) {
            ButtonType.SUCCESS -> ColorUtils.getSemanticColor(context, ColorUtils.SemanticColorType.SUCCESS)
            ButtonType.WARNING -> ColorUtils.getSemanticColor(context, ColorUtils.SemanticColorType.WARNING)
            ButtonType.DANGER -> ColorUtils.getSemanticColor(context, ColorUtils.SemanticColorType.DANGER)
            ButtonType.INFO -> ColorUtils.getSemanticColor(context, ColorUtils.SemanticColorType.INFO)
            ButtonType.NEUTRAL -> ColorUtils.getSemanticColor(context, ColorUtils.SemanticColorType.NEUTRAL)
            ButtonType.PRIMARY -> ColorUtils.getSemanticColor(context, ColorUtils.SemanticColorType.PRIMARY)
        }
        
        val textColor = currentTextColor
        return ColorUtils.isAccessibilityCompliant(textColor, backgroundColor)
    }
    
    companion object {
        /**
         * 创建语义化按钮的工厂方法
         */
        fun createSemanticButton(
            context: Context,
            type: ButtonType,
            variant: ButtonVariant = ButtonVariant.PRIMARY,
            text: String = ""
        ): SemanticButton {
            return SemanticButton(context).apply {
                setButtonType(type, variant)
                if (text.isNotEmpty()) {
                    this.text = text
                }
            }
        }
        
        /**
         * 创建成功按钮
         */
        fun createSuccessButton(context: Context, text: String = "", variant: ButtonVariant = ButtonVariant.PRIMARY): SemanticButton {
            return createSemanticButton(context, ButtonType.SUCCESS, variant, text)
        }
        
        /**
         * 创建警告按钮
         */
        fun createWarningButton(context: Context, text: String = "", variant: ButtonVariant = ButtonVariant.PRIMARY): SemanticButton {
            return createSemanticButton(context, ButtonType.WARNING, variant, text)
        }
        
        /**
         * 创建危险按钮
         */
        fun createDangerButton(context: Context, text: String = "", variant: ButtonVariant = ButtonVariant.PRIMARY): SemanticButton {
            return createSemanticButton(context, ButtonType.DANGER, variant, text)
        }
        
        /**
         * 创建信息按钮
         */
        fun createInfoButton(context: Context, text: String = "", variant: ButtonVariant = ButtonVariant.PRIMARY): SemanticButton {
            return createSemanticButton(context, ButtonType.INFO, variant, text)
        }
        
        /**
         * 创建中性按钮
         */
        fun createNeutralButton(context: Context, text: String = "", variant: ButtonVariant = ButtonVariant.PRIMARY): SemanticButton {
            return createSemanticButton(context, ButtonType.NEUTRAL, variant, text)
        }
        
        /**
         * 创建主色按钮
         */
        fun createPrimaryButton(context: Context, text: String = "", variant: ButtonVariant = ButtonVariant.PRIMARY): SemanticButton {
            return createSemanticButton(context, ButtonType.PRIMARY, variant, text)
        }
    }
}